package rursu.providers;

import java.util.List;

import rursu.business.MovieApplication;
import rursu.business.ProgressListener;
import rursu.business.Settings;
import rursu.business.pojo.Movie;
import rursu.business.pojo.MovieDetails;
import rursu.dal.loaders.ImageLoader;
import rursu.dal.loaders.MovieDetailsLoaderTask;
import rursu.dal.loaders.MoviesLoaderTask;
import rursu.dal.loaders.SearchMoviesLoaderTask;
import rursu.dal.managers.DataBaseManager;
import rursu.providers.callback.ImageLoaderCallback;
import rursu.providers.callback.MovieDetailsCallback;

public class DataProviderManager implements DataProvider, ImageProvider {

	private ProgressListener progressListener;
	private DataBaseManager dataBaseManager;
	private static ImageLoader thumbLoaderTask;
	private Settings settings;

	public ProgressListener getProgressListener() {
		return progressListener;
	}

	public void setProgressListener(ProgressListener progressListener) {
		this.progressListener = progressListener;
	}

	public void getRecentMovies(MoviesListCallback callBack) {

		// prepare url
		Integer numOfItems = (Integer) settings.getSetting(Settings.NUM_OF_ITEMS_KEY);
		String order = (String) settings.getSetting(Settings.ORDER_KEY);
		String url = MovieApplication.MOVIES_URL_STRING + "order_by=" + order;
		url += "&per_page=" + numOfItems;

		MoviesLoaderTask downLoadTask = new MoviesLoaderTask(callBack);
		downLoadTask.setProgressListener(progressListener);
		downLoadTask.setUrl(url);
		downLoadTask.setOrder(order);
		downLoadTask.setSettings(settings);
		downLoadTask.setDataBaseManager(dataBaseManager);
		downLoadTask.execute();
	}

	public void getMovieDetails(Movie movie, MovieDetailsCallback callback) {

		MovieDetailsLoaderTask downloadMovieDetailsTask = new MovieDetailsLoaderTask(callback);
		downloadMovieDetailsTask.setProgressListener(progressListener);
		downloadMovieDetailsTask.setDataBaseManager(dataBaseManager);
		downloadMovieDetailsTask.setDetailsURL(MovieApplication.MOVIE_DETAILS_URL_STRING);
		downloadMovieDetailsTask.execute(movie.getId());
	}

	public void getMoviesThumbnail(List<Movie> movies,
			ImageLoaderCallback callback) {

		Integer[] ids = new Integer[movies.size()];
		for (int i = 0; i < ids.length; i++) {
			ids[i] = movies.get(i).getId();
		}

		thumbLoaderTask = new ImageLoader();
		thumbLoaderTask.setProgressListener(progressListener);
		thumbLoaderTask.setDataBaseManager(dataBaseManager);
		thumbLoaderTask.setAllNeeded(false);
		thumbLoaderTask.setImageType("poster");
		thumbLoaderTask.setImageSize("thumb");
		thumbLoaderTask.setCallback(callback);
		thumbLoaderTask.execute(ids);

	}

	public void getMovieThumb(MovieDetails movieDetails,
			ImageLoaderCallback callback) {

		ImageLoader imageLoader = new ImageLoader();
		imageLoader.setProgressListener(progressListener);
		imageLoader.setDataBaseManager(dataBaseManager);
		imageLoader.setAllNeeded(true);
		imageLoader.setImageType("backdrop");
		imageLoader.setImageSize("thumb");
		imageLoader.setCallback(callback);
		imageLoader.execute(movieDetails.getId());

	}

	public void getPoster(MovieDetails movieDetails,
			ImageLoaderCallback callback) {

		ImageLoader imageLoader = new ImageLoader();
		imageLoader.setProgressListener(progressListener);
		imageLoader.setDataBaseManager(dataBaseManager);
		imageLoader.setAllNeeded(false);
		imageLoader.setImageType("poster");
		imageLoader.setImageSize("cover");
		imageLoader.setCallback(callback);
		imageLoader.execute(movieDetails.getId());

	}

	public void stopLoading() {

		if (thumbLoaderTask != null) {
			thumbLoaderTask.cancel(true);
			thumbLoaderTask.setProgressListener(null);
		}
	}

	public void setDALManager(DataBaseManager manager) {
		dataBaseManager = manager;
	}

	public DataBaseManager getDALManager() {
		return dataBaseManager;
	}

	public Settings getSettings() {
		return settings;
	}

	public void setSettings(Settings settings) {
		this.settings = settings;
	}

	public void getSearchMovies(MoviesListCallback callBack, String query) {
		String url = MovieApplication.MOVIE_SEARCH_URL_STRING + query;
		
		SearchMoviesLoaderTask downLoadTask = new SearchMoviesLoaderTask(callBack);
		downLoadTask.setProgressListener(progressListener);
		downLoadTask.setUrl(url);
		downLoadTask.setSettings(settings);
		downLoadTask.setQuery(query);
		downLoadTask.setDataBaseManager(dataBaseManager);
		downLoadTask.execute();
	}

}
